import { getToken, getLiveId, } from '../../utils/storage';
import { getCurrentTimestamp } from '../../utils/date';
import { getOrigin, removeOrigin } from '../../utils/storage';
import { API_HOST } from '../../assets/constant/host';
const app = getApp();

Page({
  data: {
    h5Url: '',
    activityTitle: '欢迎进入松鼠互娱',
    liveId: '',
  },
  onLoad: function (options) {
    this.setData({
      h5Url: app.globalData.h5Url,
      activityTitle: app.globalData.activityTitle,
      liveId: getLiveId(),
    });
    console.log('***h5Url***');
    console.log(this.data.h5Url);
    console.log('***activityTitle***');
    console.log(this.data.activityTitle);
  },
  onShow: function () {
    if (app.globalData.isH5NeedRefresh) {
      console.log('***h5 needRefresh***');
      app.globalData.isH5NeedRefresh = false;
      this.updateH5Url();
      removeOrigin();
      this.setData({
        h5Url: app.globalData.h5Url,
        activityTitle: app.globalData.activityTitle,
        liveId: getLiveId(),
      });
      console.log('***h5Url***');
      console.log(this.data.h5Url);
      console.log('***activityTitle***');
      console.log(this.data.activityTitle);
    }
  },
  updateH5Url() {
    const staicUrl = `${API_HOST}?liveId=${getLiveId()}&token=${getToken()}&time=${getCurrentTimestamp()}`;
    let variableUrl = '';
    const tmpOrigin = getOrigin();
    switch (tmpOrigin) {
      case 'sendGift':
        variableUrl = '&origin=sendGift';
        break;
      case 'purchaseEnterEffect':
        variableUrl = '&origin=purchaseEnterEffect';
        break;
      case 'sendBapin':
        variableUrl = '&origin=sendBapin';
        break;
      case 'sendPhoto':
        variableUrl = '&origin=sendPhoto';
        break;
      case 'sendDanmu':
        variableUrl = '&origin=sendDanmu';
        break;
      case 'sendDanmuHby':
        variableUrl = '&origin=sendDanmuHby';
        break;
      case 'editInfo':
        variableUrl = '&origin=editInfo';
        break;
      case 'sendSuperDanmu':
        variableUrl = '&origin=sendSuperDanmu';
        break;
      case 'rechargeHbkd':
        variableUrl = '&origin=rechargeHbkd';
        break;
      case 'orderSong':
        variableUrl = '&origin=orderSong';
        break;
      default:
        variableUrl = '';
    }

    if (variableUrl) {
      app.setH5Url(staicUrl + variableUrl);
      console.log(staicUrl + variableUrl);
    } else {
      app.setH5Url(staicUrl);
      console.log(staicUrl);
    }
  },
  onShareAppMessage: function () {
    return {
      title: this.data.activityTitle,
      path: '/pages/joymewIndex/joymewIndex?liveId=' + this.data.liveId,
    };
  },
})